package example;

import framework.State;
import framework.Visual;
import framework.VisualizationPlugin;

import java.util.HashMap;
import java.util.Map;

/**
 * A concrete plugin implementing VisualizationPlugin
 * 
 * @author jtumarki, ntibrewa, shuaihan, wesleyk
 * 
 */
public class VisualizationPluginImpl implements VisualizationPlugin {

	/**
	 * The table that associates each state with a visual The ID of the state is
	 * the key, and a visual is the value
	 */
	private Map<String, Visual> m = new HashMap<String, Visual>();

	/**
	 * Constructor for visualization plugin
	 * @param a AutomatonImpl used to get states
	 */
	public VisualizationPluginImpl(AutomatonImpl a) {
		mapStateToVisual(a.getAllStates());
	}

	/**
	 * The map function that associates each state with a visual
	 * @param array of all states
	 * @return Map mapping ids to visuals
	 */
	@Override
	public Map<String, Visual> mapStateToVisual(State[] states) {
		State[] s = states;
		
		if(states == null) {
			throw new IllegalArgumentException("the set of states cannot be null");
		}

		for (int i = 0; i < s.length; i++) {

			if (s[i] instanceof AliveState) {
				m.put(s[i].getID(), new AliveVisual());
			} else if (s[i] instanceof DeadState) {
				m.put(s[i].getID(), new DeadVisual());
			} else
				throw new IllegalArgumentException("the state is not legal");
		}
		return m;
	}

	/**
	 * A convenient function to get the visual
	 * @param s State that user wants the visual of
	 * @return Visual associated to State s
	 */
	@Override
	public Visual getVisual(State s) {
		if (s == null || !m.containsKey(s.getID()))
			throw new IllegalArgumentException(
					"the state is not mapped to a visual");
		Visual result = m.get(s.getID());
		return result;
	}

}
